package tag11_24_07.matrix;

import java.util.Random;

public class IntMatrix {

	// 5.-------------------------------------
	static IntMatrix getRandomMatrix(int rows, int cols, int maxValue){
		IntMatrix m = new IntMatrix(rows, cols);
		
		Random random = new Random();
		
		for (int i = 0; i < m.getRowsCount(); i++) {
			for (int j = 0; j < m.getColsCount(); j++) {
				int zufallszahl = random.nextInt(maxValue+1);
				m.data[i][j] = zufallszahl; 
			}
		}
		
		
		return m;
	}
	
	// 7.------------------------------------
	static IntMatrix transponieren(int row, int cols, int rango){
		System.out.println("Gegeben Matrix");
		IntMatrix im = IntMatrix.getRandomMatrix(row, cols, rango);
		System.out.println(im);
		IntMatrix imt = new IntMatrix(cols, row); 
		for (int i = 0; i < im.getRowsCount(); i++) {
			for (int j = 0; j < im.getColsCount(); j++) {
				imt.data[j][i] = im.data[i][j];
			}
		}		
		return imt;
	}
	
	//8. ------------------------------------
	static IntMatrix addieren(int row, int cols, int rango, int add){
		System.out.println("Gegebene Matrix");
		IntMatrix im = IntMatrix.getRandomMatrix(row, cols, rango);
		System.out.println(im);
		for (int i = 0; i < im.getRowsCount(); i++) {
			for (int j = 0; j < im.getColsCount(); j++) {
				im.data[i][j] += im.data[i][j]; 
			}
		}
		return im;
	}
	
	// 9. ------------------------------------
	static IntMatrix multiplizieren(int row, int cols, int rango, int mult){
		System.out.println("Gegebene Matrix");
		IntMatrix im = IntMatrix.getRandomMatrix(row, cols, rango);
		System.out.println(im);
		for (int i = 0; i < im.getRowsCount(); i++) {
			for (int j = 0; j < im.getColsCount(); j++) {
				im.data[i][j] *= im.data[i][j]; 
			}
		}
		return im;
	}

	//Variablen
	private int[][] data;

	
	// 1.------------------------------------
	IntMatrix(int rows, int cols) {
		if (rows < 1 || cols < 1) {
			System.out
					.println("Fehler in Konstruktor der IntMatrix. Falsche Dimensionen: "
							+ rows + " X " + cols);
			System.exit(0);
		}
		data = new int[rows][cols];
	}

	//2.------------------------------------
	public String toString() {

			String back = "";

			for (int i = 0; i < data.length; i++) {
				for (int j = 0; j < data[i].length; j++) {
					back = back + data[i][j] + ", ";
				}

				if (back.endsWith(", ")) {
					back = back.substring(0, back.length() - 2);
				}

				back += "\n";
			}

			return back;
		}
	
	// 3. -----------------------------------
	IntMatrix(int rows, int cols, int initValue) {
		this(rows, cols); // Aufruf des Konstruktors(int, int)

		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				data[i][j] = initValue;
			}
		}
	}

	
	// 4. -----------------------------------
	/**
	 * 
	 * @param row
	 *            0 ... Anzahl Zeilen - 1
	 * @param col
	 *            0 ... Anzahl Spalten - 1
	 * */
	
	int get(int row, int col) {
		if (row < 0 || row >= getRowsCount()) {
			System.out.println("fehler! Falscher row-Index: " + row
					+ ", Erlaub: 0 ..." + (getRowsCount() - 1));
			System.exit(0);
		}
		
		if( col < 0 || col >= getColsCount() ){
			System.out.println("fehler! Falscher row-Index: " + row
					+ ", Erlaub: 0 ..." + (getColsCount() - 1));
			System.exit(0);
		}
		return data[row][col];
	}

	int getRowsCount() {
		return data.length;
	}

	int getColsCount() {
		return data[0].length;
	}
	
	
	void set(int row, int col){
		data[row][col] = 5;
	}
	

	// 6. ----------------------------------
	boolean equals(IntMatrix m){
		
		if(getRowsCount() != m.getRowsCount())
			return false;
		
		if(getColsCount() != m.getColsCount()){
			return false;
		}
		
		for (int i = 0; i < getRowsCount(); i++) {
			for (int j = 0; j < getColsCount(); j++) {
				if(get(i,j) != m.get(i,j))
					return false;
			}
		}
		return true;
	}
	
	

}